
/* 
NAME: rgi.do
USING .dta file(s): 
	rgi.dta
	personal.dta
	noc.dta

USING .do file(s): NONE

DESCRIPTION: This program merges data together, creates variable transformations (including 
	a personalism index), and analyses the relationship between personalism index and the
	Resourse Governance Index (RGI)

AUTHOR: Joseph Wright
ORIGIN DATE: 11.20.15
*/

set scheme lean2

* Merge data sets  *
	cd "$dir\corruption"
	use temp,clear
	sort gwf_country
	save, replace
	use noc, clear
	replace country = "Congo/Zaire" if country == "Congo DRC"
	replace country = "Congo-Brz" if country == "Congo Br"
	sort country
	save, replace
	use rgi, clear
	replace country = "Congo/Zaire" if country == "Congo (DRC)"
	sort country
	merge country using noc
	tab _merge
	rename _merge mergeR1
	rename country gwf_country  
	replace gwf_country ="UAE" if gwf_country=="United Arab Emirates"
	sort gwf_country
	merge gwf_country using temp
	tab _merge
	rename _merge mergeR2
	list gwf_country if mergeR2==1,clean noobs
	egen meangdp = mean(lgdpcap),by(cow)
	gen xoilpc = oilpc if year==2010
	egen meanoil  = mean(xoilpc),by(cow)
	replace meanoil = 6.609899 if country=="Iraq" /* 2003 value */


* Create personalism index: country-mean of 3 indicators, weighted by autocratic years
	global yr = 1979
	egen minyr = min(year) if year>$yr, by(cow)
	*replace minyr =  $yr + 1 if country=="Venezuela" | country =="Philippines"  /* not 2006 or 1972*/
	egen pmaxall = mean(pers) if year>$yr & year<2011, by(cow) 
	egen count = count(pers) if year>$yr & year<2011, by(cow) 
	gen yrs = count/(2011-minyr)
	replace pmaxall = pmaxall*yrs
	egen m = max(pmaxall),by(cow)
	replace pmaxall = m 
	drop m
 
* Personalism and resource governance *
	egen ctag1 = tag(cow) if composite~=. & pmaxall~=. & gwf_country~="Norway"
	gen carbon = resourcemeasure == "Hydrocarbons"
	local g ="composit institutionalandlegalsetting reportingpractices safeguardsandqualitycontrols enablingenvironment"
	foreach c of local g {
		gen y`c' = `c'/100
		gen x`c' = logit(`c'/100)
		spearman `c' pmaxall if ctag1==1
		qui: reg x`c' pmaxall if ctag1==1,r
		lincom pmaxall
		qui: glm y`c' pmaxall if ctag==1,fam(bin) link(logit) vce(r)
		lincom pmaxall
		qui: glm y`c' pmaxall carbon meangdp meanoil if ctag==1,fam(bin) link(logit) vce(r)
		lincom pmaxall
	}	

* Personalism RGI plot, composite *
	twoway lfit composit pmax if ctag1==1,   /*
	*/ scheme(lean2) ytitle(Composite score,size(medsmall)) xtitle(Personalism,size(medsmall)) title(Bivariate correlation,size(medium)) /*
	*/ ylab(0 (20) 100,glcolor(gs15)) xscale(range(-.05 .9)) yscale(range(0 100)) || /*
	*/ scatter composit pmax if ctag1==1, msymbol(oh) mlab(StateAB) /*
	*/ xsize(3) ysize(2.5)  legend(size(small) label(3 "Observed") pos(6) ring(1) col(3)) 
	graph export "$dir\golden\Personal-RGI.pdf", as(pdf)   replace
	graph export "$dir\golden\ISQ-Figure-9.1.png", as(png)   replace

 * Personalism RGI plot, composite, drop Turk *
	twoway lfit composit pmax if ctag1==1 & country~="Turkmenistan" & country~="Myanmar",   /*
	*/ scheme(lean2) ytitle(Composite score,size(medsmall)) xtitle(Personalism,size(medsmall)) title(Bivariate correlation,size(medsmall)) /*
	*/ ylab(0 (20) 100,glcolor(gs15)) xscale(range(-.05 .9)) yscale(range(0 100)) || /*
	*/ scatter composit pmax if ctag1==1 & country~="Turkmenistan" & country~="Myanmar", msymbol(oh) mlab(StateAB) /*
	*/ xsize(3) ysize(2.5)  legend(size(small) label(3 "Observed") pos(6) ring(1) col(3)) 
	graph export "$dir\golden\Personal-RGI-no-outliers.pdf", as(pdf)   replace

	* Each sub-component of RGI *
	twoway qfitci instit pmax if ctag1==1, saving(t2, replace) ciplot(rline) level(90)  /*
	*/ scheme(lean2) ytitle(Institutions) xtitle("") /*
	*/ ylab(0 (20) 80,glcolor(gs15)) yscale(range(0 80)) || /*
	*/ scatter instit pmax if ctag1==1, msymbol(oh) legend(off)
	twoway qfitci reporting pmax if ctag1==1, saving(t3, replace) ciplot(rline) level(90)  /*
	*/ scheme(lean2) ytitle(Reporting) xtitle("") /*
	*/ ylab(0 (20) 80,glcolor(gs15)) yscale(range(0 80)) || /*
	*/ scatter reporting pmax if ctag1==1, msymbol(oh) legend(off)
	twoway qfitci safeg pmax if ctag1==1, saving(t4, replace) ciplot(rline) level(90)  /*
	*/ scheme(lean2) ytitle(Safeguards) xtitle("") /*
	*/ ylab(0 (20) 80,glcolor(gs15)) yscale(range(0 80)) || /*
	*/ scatter safeg pmax if ctag1==1, msymbol(oh)legend(off)
	twoway qfitci enabl pmax if ctag1==1, saving(t5, replace) ciplot(rline) level(90)  /*
	*/ scheme(lean2) ytitle(Enabling) xtitle("") /*
	*/ ylab(0 (20) 80,glcolor(gs15)) yscale(range(0 80)) || /*
	*/ scatter enabl pmax if ctag1==1, msymbol(oh)  legend(off)
	graph combine t2.gph t3.gph t4.gph t5.gph, col(2) xsize(4) ysize(4) xcommon scheme(lean2) b1(Personalism)
	graph export "$dir\golden\Personal-RGI-Items.pdf", as(pdf)   replace
	erase t2.gph
	erase t3.gph
	erase t4.gph
	erase t5.gph

* Personalism and Ownership structure *
	egen ctag2 = tag(cow) if regnoc~=. & pmax~=.  & gwf_country~="Norway"
	label define xnoc 0 "No NOC/Non-Reg" 1 "Regulatory NOC"
	label val regnoc xnoc
	sum pmax if ctag2==1, detail
	gen highp = pmax>.2
	tab highp regnoc if ctag2==1, row
	table regnoc if ctag2==1, c(n pmax mean pmax median pmax)
 
* Resource governance, Ownership, and Personalism *
	gen lxcomp = ln(1+abs(xcomp))
	replace lxcomp = lxcomp*-1 if xcomp<=0
	egen ctag3 =tag(cow) if  pmax~=. & comp~=.  & gwf_country~="Norway"
	reg lxcomp  carbon meangdp meanoil pmax if ctag3==1,r
	avplot pmax,ylab(-1 (1) 1,glcolor(gs15)) mlab(StateAB) mlabpos(12) xscale(range(-.4 .4)) /*
	*/ xtitle("e(Personalism|X)") ytitle("e(Composite RGI score|X)")
	graph export "$dir\golden\Personal-RGI-Avplot.pdf", as(pdf)   replace
	reg lxcomp  carbon meangdp meanoil pmax if ctag3==1 & country~="Turkmenistan",r
	avplot pmax,ylab(-1 (1) 1,glcolor(gs15)) mlab(StateAB) mlabpos(6) xscale(range(-.4 .4)) /*
	*/ xtitle("e(Personalism|X)") ytitle("e(Composite RGI score|X)")
	graph export "$dir\golden\Personal-RGI-Avplot-No-Turkmenistan.pdf", as(pdf)   replace
	
	
	*********************
	** Reported models **
	*********************
		* Base model * 
	gen comp  = comp/100
	glm comp pmax meangdp carbon meanoil if ctag1==1 , fam(bin) link(logit) vce(r)
	est store x1
	glm comp pmax meangdp carbon if ctag1==1 & regnoc~=., fam(bin) link(logit) vce(r)
	krls comp pmax meangdp carbon if ctag1==1
  		* Add NOC *
	glm comp pmax meangdp carbon meanoil regnoc if ctag1==1, fam(bin) link(logit) vce(r)
	est store x2
	krls comp pmax meangdp carbon meanoil regnoc if ctag1==1
		* Drop carbon *
	tab carbon if e(sample)
	glm comp pmax meangdp meanoil regnoc if ctag1==1, fam(bin) link(logit) vce(r)
 	krls comp pmax meangdp meanoil regnoc if ctag1==1
    krls comp pmax regnoc if e(sample)==1
    krls comp regnoc if e(sample)==1
	krls comp pmax if e(sample)==1
	
	label var regnoc "Regulatory NOC"
 	label var pmax "{bf:Personalism}"
	label var carbon "Hydrocarbons"
	label var meangdp "GDP pc (log)"
	label var meanoil "Oil rents (log)"
	coefplot (x1,  msymbol(T) mcolor($color1) ciopts(lpat(solid)lcol($color1 $color1))) (x2, msymbol(S) mcolor($color3) ciopts(lpat(solid)lcol($color3 $color3))) , /*
			*/ title("Conditional correlation",size(medium))  order(pmaxall regnoc meangdp carbon) /*
			*/ scheme(lean2) drop(_cons) xlab(-2 (1) 1) xline(0, lpattern(dash)) grid(glcolor(gs15)) mfcolor(white) /*
			*/ ysize(2.5) xsize(2.85)  mlabel format(%9.1f) mlabsize(vsmall) mlabposition(11) mlabgap(*1) /*
			*/ legend(label(3 "45 countries") label(6 "31 countries") size(small)  pos(6) ring(1.5) col(2))  /*
			*/ levels(95 90) xtitle("  Coefficient estimate", height(6) size(medsmall))    
	graph export "$dir\golden\Personal-RGI-Controls.pdf", as(pdf)   replace
	graph export "$dir\golden\ISQ-Figure-9.2.png", as(png)   replace

	gen xregnoc = regnoc
	recode xregnoc (.=0)
	glm comp xregnoc carbon pmax meangdp meanoil if ctag1==1, fam(bin) link(logit) vce(r)

	*** Table reported in Appendix ***
	replace meanoil = meanres if meanoil==.
	sum yinstitutionalandlegalsetting yreportingpractices ysafeguardsandqualitycontrols yenablingenvironment
	glm yinstitutionalandlegalsetting pmax meangdp carbon meanoil  if ctag1==1, fam(bin) link(logit) vce(r)
	glm yreportingpractices pmax meangdp carbon meanoil  if ctag1==1, fam(bin) link(logit) vce(r)
	glm ysafeguardsandqualitycontrols pmax meangdp carbon meanoil  if ctag1==1, fam(bin) link(logit) vce(r)
	glm yenablingenvironment pmax meangdp carbon meanoil  if ctag1==1, fam(bin) link(logit) vce(r)
 
	
***********   The End  *************

